package cn.zhangjie.order.controller;

import cn.zhangjie.basic.util.JsonResult;
import cn.zhangjie.basic.util.LoginContext;
import cn.zhangjie.basic.util.PageList;
import cn.zhangjie.order.domain.OrderPetAcquisition;
import cn.zhangjie.order.dto.OrderDto;
import cn.zhangjie.order.query.OrderPetAcquisitionQuery;
import cn.zhangjie.order.service.IOrderPetAcquisitionService;
import cn.zhangjie.user.domain.Logininfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (OrderPetAcquisition)表控制层
 *
 * @author makejava
 * @since 2022-08-11 11:05:51
 */
@RestController
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {
    /**
     * 注入对象
     */
    @Autowired
    private IOrderPetAcquisitionService orderPetAcquisitionService;

    /**
     * 分页查询
     * @return 查询结果
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    public PageList<OrderPetAcquisition> queryPage(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request) {
	try {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return orderPetAcquisitionService.queryPage(query,logininfo.getId());
	} catch (Exception e) {
		e.printStackTrace();
        return new PageList<>();
	    }
    }

    /**
     * 分页查询
     * @return 查询结果
     */
    @PostMapping("/confirm")
    @ApiOperation(value = "分页查询接口")
    public JsonResult confirm(@RequestBody OrderDto dto) {
        try {
            orderPetAcquisitionService.confirm(dto);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("订单确认失败");
        }
    }


    /**
     * 查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping()
    @ApiOperation(value = "查询所有")
    public List<OrderPetAcquisition> findAll() {
	    return orderPetAcquisitionService.findAll();
    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "取消订单")
    public JsonResult delete(@PathVariable("id") Long id) {
        try {
		orderPetAcquisitionService.cancel(id);
		return JsonResult.me();
	} catch (Exception e) {
		e.printStackTrace();
        return JsonResult.me().setMsg("订单取消失败");
	    }
    }





}

